package com.xiaoxing.gongqiju.mvp.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoxing.gongqiju.R;
import com.xiaoxing.gongqiju.R2;
import com.xiaoxing.gongqiju.di.component.DaggerLiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuComponent;
import com.xiaoxing.gongqiju.di.module.LiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuModule;
import com.xiaoxing.gongqiju.mvp.contract.LiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuContract;
import com.xiaoxing.gongqiju.mvp.presenter.LiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuPresenter;
import com.xiaoxing.gongqiju.mvp.ui.adapter.LiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuAdapter;
import com.xiaoxing.gongqiju.mvp.ui.entity.LiaoCheng_GongQiJu_ShenQingChuKu;
import com.xiaoxing.gongqiju.mvp.ui.entity.LiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJu;
import com.xw.repo.XEditText;

import org.jetbrains.annotations.NotNull;
import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import com.jess.arms.utils.CProgressDialogUtils;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonres.view.RecycleViewDivider;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import okhttp3.RequestBody;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.GONGQIJU_LIAOCHENG_ACTIVITY_LIAO_CHENG_GONG_QI_JU_SHEN_QING_CHU_KU_TI_JIAO_SHU_JU)
public class LiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuActivity extends BaseActivity<LiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuPresenter> implements LiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuContract.View, OnRefreshListener {


    private static boolean mIsNeedDemo = true;
    @BindView(R2.id.empty_text)
    TextView empty_text;
    @BindView(R2.id.empty_image)
    ImageView empty_image;

    @BindView(R2.id.empty)
    View mEmptyLayout;
    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R2.id.refreshLayout)
    RefreshLayout mRefreshLayout;
    @BindView(R2.id.btnRight)
    TextView mBtnRight;
    @BindView(R2.id.xet_na_huo_ren_xing_ming)
    XEditText xetNaHuoRenXingMing;
    @BindView(R2.id.xet_na_huo_ren_shou_ji_hao)
    XEditText xetNaHuoRenShouJiHao;
    private LiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuAdapter mAdapter;
    private List<LiaoCheng_GongQiJu_ShenQingChuKu.DataBean> mDataBeanList = new ArrayList<>();
    private HashMap<String, LiaoCheng_GongQiJu_ShenQingChuKu.DataBean> mSelectData;
    private View.OnClickListener mRightListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            getLiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuList();
        }
    };

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerLiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .liaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuModule(new LiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_liao_cheng__gong_qi_ju__shen_qing_chu_ku_ti_jiao_shu_ju; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_liao_cheng__gong_qi_ju__shen_qing_chu_ku_ti_jiao_shu_ju));
        mBtnRight.setText("提交数据");
        mBtnRight.setOnClickListener(mRightListener);
        mSelectData = (HashMap<String, LiaoCheng_GongQiJu_ShenQingChuKu.DataBean>) getIntent().getExtras().getSerializable("selectData");

        if (mSelectData != null) {
            for (Map.Entry<String, LiaoCheng_GongQiJu_ShenQingChuKu.DataBean> entry : mSelectData.entrySet()) {
                mDataBeanList.add(entry.getValue());
            }
        }

        initRefreshLayout();
        initRecyclerView();
        initEmpty();
        if (mDataBeanList != null && mDataBeanList.size() > 0) {
            mEmptyLayout.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.VISIBLE);
        } else {
            mEmptyLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        }

    }


    private void initEmpty() {
        empty_image.setImageResource(R.drawable.ic_empty);
        empty_text.setTextColor(Color.parseColor("#D8D8D8"));
        empty_text.setText("暂无数据");
    }



    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(LiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuActivity.this));
        mRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, 10, Color.parseColor("#EEEEEE")));

        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new LiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuAdapter(LiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuActivity.this, mDataBeanList));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            }
        });
    }

    private void initRefreshLayout() {
        mRefreshLayout.setRefreshHeader(new
                ClassicsHeader(this).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(R.color.public_colorPrimary).setAccentColorId(android.R.color.white));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.autoRefresh();
        mRefreshLayout.setEnableLoadMore(false);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

        mRefreshLayout.finishRefresh();
        mEmptyLayout.setVisibility(View.GONE);

    }

    @Override
    public void getLiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuDataSuccess(LiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJu entityList) {

//        if (entityList != null && entityList.getData() != null && entityList.getData().size() > 0) {
//            mDataBeanList.clear();
//            mDataBeanList.addAll(entityList.getData());
//            mAdapter.notifyDataSetChanged();
//            mEmptyLayout.setVisibility(View.GONE);
//            mRecyclerView.setVisibility(View.VISIBLE);
//        } else {
//            mEmptyLayout.setVisibility(View.VISIBLE);
//            mRecyclerView.setVisibility(View.GONE);
//        }

        showMessage(entityList.getMessage());
        if (entityList.getCode().equals("200")) {
            EventBus.getDefault().post("", "updateLiaoCheng_GongQiJu_ShenQingChuKuList");
            killMyself();
        }
    }

    private void getLiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuList() {

        if (TextUtils.isEmpty(getNaHuoRenXingMing())) {
            showMessage("请输入拿货人姓名");
            return;
        }
        if (TextUtils.isEmpty(getNaHuoRenShouJiHao())) {
            showMessage("请输入拿货人手机号");
            return;
        }
        String item = "";
        for (Map.Entry<String, LiaoCheng_GongQiJu_ShenQingChuKu.DataBean> entry : mSelectData.entrySet()) {
            LiaoCheng_GongQiJu_ShenQingChuKu.DataBean dataBean = entry.getValue();
            item += "{\n" +
                    "    \"opid\": 0,\n" +
                    "    \"mtid\": " + dataBean.getMtid() + ",\n" +
                    "    \"OutNum\": " + dataBean.getSelectNum() + ",\n" +
                    "    \"applyuserid\": " + mSharedPreferencesHelper.getString(BaseConstants.UID) + ",\n" +
                    "    \"applydatetime\": \"" + "" + "\",\n" +
                    "    \"checkuserid\": " + "" + ",\n" +
                    "    \"checkdatetime\": \"" + "" + "\",\n" +
                    "    \"checkresult\": 0,\n" +
                    "    \"remark\": \"" + "" + "\",\n" +
                    "    \"recevieusername\": \"" + getNaHuoRenXingMing() + "\",\n" +
                    "    \"receiveusephone\": \"" + getNaHuoRenShouJiHao() + "\"\n" +
                    "  },";
        }


        String jsonData = "[" + ArmsUtils.cutLastString(item) + " ]";
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonData);
//        HashMap<String, String> map = new HashMap<>();

//        map.put("opid", "0");
//        map.put("mtid", "");
//        map.put("OutNum", "");
//        map.put("recevieusername", getNaHuoRenXingMing());
//        map.put("receiveusephone", getNaHuoRenShouJiHao());
//        map.put("checkresult", "");


        mPresenter.getLiaoCheng_GongQiJu_ShenQingChuKuTiJiaoShuJuList(body);
    }

    @NotNull
    private String getNaHuoRenXingMing() {
        return xetNaHuoRenXingMing.getText().toString().trim();
    }

    @NotNull
    private String getNaHuoRenShouJiHao() {
        return xetNaHuoRenShouJiHao.getText().toString().trim();
    }


    @Override
    public void showLoading() {
        CProgressDialogUtils.showProgressDialog(this);
    }

    @Override
    public void hideLoading() {
        CProgressDialogUtils.cancelProgressDialog(this);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);

    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

}
